package org.makcro.sms.services.chat.web.client.mvc.view;

import org.makcro.sms.services.chat.web.client.AppEvents;
import org.makcro.sms.services.chat.web.client.widgets.LoginDialog;

import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;

public class LoginView extends View {

	private LoginDialog loginDialog;
	
	public LoginView(Controller controller) {
		super(controller);
	}
	
	@Override
	protected void initialize() {
		super.initialize();
		loginDialog = new LoginDialog();
		
	}

	@Override
	protected void handleEvent(AppEvent event) {
		
		if(event.getType() == AppEvents.ShowLoginDialog){
			onLogin();
		}
		else if(event.getType() == AppEvents.LoginFailed){
			onFailedLogin();
		} 
		else if(event.getType() == AppEvents.LoginSuccessful){
			onSuccessfullLogin();
		}
	}

	private void onSuccessfullLogin() {
		loginDialog.hide();
		
	}

	private void onFailedLogin() {
		loginDialog.getButtonBar().enable();
		loginDialog.reset();
		loginDialog.getTxtUsername().focus();
	}

	private void onLogin() {
		loginDialog.show();
		
	}

}
